package com.project.lvshi.controller.time;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.lvshi.domain.Result;
import com.project.lvshi.domain.SysTime;
import com.project.lvshi.domain.User;
import com.project.lvshi.enums.ResultCode;
import com.project.lvshi.service.SysTimeService;
import com.project.lvshi.service.UserService;
import com.project.lvshi.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 律师时间安排controller
 * @date 2024/04/18 10:39
 */
@Controller
@ResponseBody
@RequestMapping("time")
public class SysTimeController {

    @Autowired
    private SysTimeService sysTimeService;
    @Autowired
    private UserService userService;

    /** 分页获取律师时间安排 */
    @PostMapping("getSysTimePage")
    public Result getSysTimePage(@RequestBody SysTime sysTime) {
        Page<SysTime> page = new Page<>(sysTime.getPageNumber(),sysTime.getPageSize());
        QueryWrapper<SysTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(sysTime.getContent()),SysTime::getContent,sysTime.getContent())
                .eq(StringUtils.isNotBlank(sysTime.getUserId()),SysTime::getUserId,sysTime.getUserId())
                .eq(StringUtils.isNotBlank(sysTime.getUserName()),SysTime::getUserName,sysTime.getUserName())
                .orderByDesc(SysTime::getTime);
        Page<SysTime> sysTimePage = sysTimeService.page(page, queryWrapper);
        return Result.success(sysTimePage);
    }

    /** 根据id获取律师时间安排 */
    @GetMapping("getSysTimeById")
    public Result getSysTimeById(@RequestParam("id")String id) {
        SysTime sysTime = sysTimeService.getById(id);
        return Result.success(sysTime);
    }

    /** 保存律师时间安排 */
    @PostMapping("saveSysTime")
    public Result saveSysTime(@RequestBody SysTime sysTime) {
        String id = TokenUtils.getUserIdByToken();
        User user = userService.getById(id);
        sysTime.setUserId(id);
        sysTime.setUserName(user.getUserName());
        boolean save = sysTimeService.save(sysTime);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑律师时间安排 */
    @PostMapping("editSysTime")
    public Result editSysTime(@RequestBody SysTime sysTime) {
        boolean save = sysTimeService.updateById(sysTime);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除律师时间安排 */
    @GetMapping("removeSysTime")
    public Result removeSysTime(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                sysTimeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("律师时间安排id不能为空！");
        }
    }

}
